//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================
// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace EntLibContrib.Logging.Configuration
{
	/// <summary>
	/// Container for the file properties when the class does not implement <see cref="IFileTraceListenerData"/>.
	/// </summary>
	public class FileTraceListenerData : IFileTraceListenerData
	{
		#region Construction
		/// <summary>
		/// Constructor to initialize a new instance of the class.
		/// </summary>
		/// <param name="traceData">The trace data.</param>
		public FileTraceListenerData(FlatFileTraceListenerData traceData)
		{
			dataFileName = traceData.FileName;
			dataFooter = traceData.Footer;
			dataFormatter = traceData.Formatter;
			dataHeader = traceData.Header;
			dataName = traceData.Name;
		}

		/// <summary>
		/// Constructor to initialize a new instance of the class.
		/// </summary>
		/// <param name="traceData">The trace data.</param>
		public FileTraceListenerData(RollingFlatFileTraceListenerData traceData)
		{
			dataFileName = traceData.FileName;
			dataFooter = traceData.Footer;
			dataFormatter = traceData.Formatter;
			dataHeader = traceData.Header;
			dataName = traceData.Name;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Get common file properties if the derived class has them.
		/// </summary>
		/// <param name="traceData">The trace data.</param>
		/// <returns>common file properties</returns>
		public static IFileTraceListenerData GetFileData(TraceListenerData traceData)
		{
			IFileTraceListenerData fileData = traceData as IFileTraceListenerData;
			AsFlatFileTraceListenerData(traceData, ref fileData);
			AsRollingFlatFileTraceListenerData(traceData, ref fileData);
			return fileData;
		}
		#endregion

		#region Private Methods
		private static void AsFlatFileTraceListenerData(TraceListenerData traceData, ref IFileTraceListenerData fileData)
		{
			if (fileData == null)
			{
				FlatFileTraceListenerData fileFlat = traceData as FlatFileTraceListenerData;
				if (fileFlat != null)
					fileData = new FileTraceListenerData(fileFlat);
			}
		}

		private static void AsRollingFlatFileTraceListenerData(TraceListenerData traceData, ref IFileTraceListenerData fileData)
		{
			if (fileData == null)
			{
				RollingFlatFileTraceListenerData fileFlat = traceData as RollingFlatFileTraceListenerData;
				if (fileFlat != null)
					fileData = new FileTraceListenerData(fileFlat);
			}
		}
		#endregion

		#region IFileTraceListenerData Members
		private string dataFileName;
		private string dataFooter;
		private string dataFormatter;
		private string dataHeader;
		private string dataName;

		/// <summary>
		/// File name to write the log entry into.
		/// </summary>
		/// <value></value>
		public string FileName
		{
			get { return dataFileName; }
		}

		/// <summary>
		/// Footer to write after the log entry.
		/// </summary>
		/// <value></value>
		public string Footer
		{
			get { return dataFooter; }
		}

		/// <summary>
		/// Name of the formatter that will format the log entry.
		/// </summary>
		/// <value></value>
		public string Formatter
		{
			get { return dataFormatter; }
		}

		/// <summary>
		/// Header to write before the log entry.
		/// </summary>
		/// <value></value>
		public string Header
		{
			get { return dataHeader; }
		}

		/// <summary>
		/// Name of the trace listener instance.
		/// </summary>
		/// <value></value>
		public string Name
		{
			get { return dataName; }
		}

		#endregion
	}
}
